const app = getApp();
Page({
  data: {
    pagenum: 1,
    pagesize: 5,
    more: 0,
    imgUrl: app.globalData.imgUrl,
    localImg: app.globalData.staticimgUrl,  // 本地图片
    replyCommentId: '',
    replyPeople: '',
    replyContent: ''
  },

  onLoad: function(options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      'goodsid': options.id,
    })
    this.loadData();
  },

  // 加载数据
  loadData: function() {
    var that = this;
    app.request({
      url: 'shopp/goodsCommentApi/getServiceGoodsCommentPage',
      data: {
        goodsid: this.data.goodsid, // 产品id
        pagenum: that.data.pagenum, // 页码
        pagesize: that.data.pagesize, // 每页个数
      },
      success: function(res) {
        if (res.data) {
          if (that.data.more == 0) {
            that.setData({
              'info': res.data,
            })
          } else {
            wx.hideLoading();
            that.setData({
              more: 0,
            })
            if (res.data.length) {
              var info = that.data.diaryinfo.concat(res.data);
              that.setData({
                'info': info,
              })
            } else {
              wx.showToast({
                title: '已无更多...',
                icon: 'none',
                duration: 1000,
              });
            }
          }
        }
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      'more': 1,
      'pagenum': this.data.pagenum + 1,
    });
    this.loadData();
  },
  checkReplyCommment: function (e) {
    let replyCommentId = e.currentTarget.dataset.commentId;
    let replyPeople = e.currentTarget.dataset.replyPeople;
    this.setData({
      replyCommentId,
      replyPeople: `回复${replyPeople}`
    });
  },
  changeReplyContent: function (e) {
    let replyContent = e.detail.value;
    this.setData({
      replyContent
    });
  },
  bindReply: function () {
    const that = this;
    let { replyCommentId, replyContent } = this.data;
    if (replyCommentId) {
      if (replyContent) {
        app.request({
          url: 'shopp/goodsCommentApi/replyComment',
          data: {
            commentid: replyCommentId,
            replycontent: replyContent
          },
          success: function (res) {
            wx.showToast({
              title: '回复完成',
              icon: 'none',
              duration: 1000,
              success: function () {
                that.loadData();
                that.clearReplyData();
              }
            });
          }
        })
      }
      else {
        wx.showToast({
          title: '请输入回复内容',
          icon: 'none',
          duration: 1000
        });
      }
    }
    else {
      wx.showToast({
        title: '请选择需要回复的评价',
        icon: 'none',
        duration: 1000
      });
    }
  },
  clearReplyData: function () {
    this.setData({
      replyCommentId: '',
      replyPeople: '',
      replyContent: ''
    });
  }

})